import { onBeforeMount, onUnmounted, onActivated, onDeactivated } from "vue";
import mitter from "@/plugins/mitt";
import { useRoute } from "vue-router";

export default function (fn: () => void) {
	const keepAlive = useRoute().meta.keepAlive;

	function watchUpdate() {
		mitter.$on("update-page", fn);
	}
	onActivated(() => {
		keepAlive && watchUpdate();
	});
	onDeactivated(() => {
		mitter.$off("update-page", fn);
	});
	onBeforeMount(() => {
		!keepAlive && watchUpdate();
	});
	onUnmounted(() => {
		mitter.$off("update-page", fn);
	});
}
